#include "CSurface.h"
#include "SDL_image.h"
 
CSurface::CSurface() {
}
 SDL_Surface* CSurface::OnLoad(char* File) {
    SDL_Surface* Surf_Temp = nullptr;
    SDL_Surface* Surf_Return = nullptr;
 
	if((Surf_Temp = IMG_Load(File)) == nullptr) {
        return nullptr;
    }
 
    Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp);
    SDL_FreeSurface(Surf_Temp);
 
    return Surf_Return;
}
 
bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y) {
    if(Surf_Dest == nullptr || Surf_Src == nullptr) {
        return false;
    }
 
    SDL_Rect DestR;
 
    DestR.x = X;
    DestR.y = Y;
 
    SDL_BlitSurface(Surf_Src, NULL, Surf_Dest, &DestR);
 
    return true;
}

bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H) {
    if(Surf_Dest == nullptr || Surf_Src == nullptr) {
        return false;
    }
 
    SDL_Rect DestR;
 
    DestR.x = X;
    DestR.y = Y;
 
    SDL_Rect SrcR;
 
    SrcR.x = X2;
    SrcR.y = Y2;
    SrcR.w = W;
    SrcR.h = H;
 
    SDL_BlitSurface(Surf_Src, &SrcR, Surf_Dest, &DestR);
 
    return true;
}

// Make some color to Transparent
bool CSurface::Transparent(SDL_Surface* Surf_Dest, int R, int G, int B)
{
	if(Surf_Dest == nullptr)
		return false;

	//3 dahi param-r shuud ongo-g n oruulj ogohgui bn. Uunii orond SDL_MapRGB-g
	//ashiglaj bn. Uchir n SDL_MapRGB n Surf_Dest-s R,G,B-tei hamgiin tostei ongiig n gargaj avna
	SDL_SetColorKey(Surf_Dest, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(Surf_Dest->format, R,G,B));

	return true;
}
